// 配置药品相关接口
const express = require('express')
const router = express.Router()
const Response = require('../utils/Response.js')

// 引入mysql连接池
const pool = require('../utils/db.js')
const { error } = require('../utils/Response.js')

// 查询所有药品
router.get("/drug-all", async (req, resp) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize } = req.query;
  // 执行查询数组业务
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from drug limit ?,?";
    let result = await pool.querySync(sql, [startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from drug";
    let result2 = await pool.querySync(sql2, [startIndex, size]);
    let total = result2[0].count;
    resp.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 根据药品ID查询药品信息
router.post('/drugById', (req, res) => {
  let { drug_id } = req.body
  let sql = 'select * from drug where drug_id=?'
  pool.query(sql, [drug_id], (err, result) => {
    if (err) {
      res.send(Response.error(500, err))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据药品名称模糊查询药品信息
router.post('/drugByName', (req, res) => {
  let { drug_name } = req.body
  let sql = `select * from drug where drug_name like '%${drug_name}%'`
  pool.query(sql, [drug_name], (err, result) => {
    if (err) {
      res.send(Response.err(500, err))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据药品分类进行查询
router.get('/drugByCategory',(req,res) =>{
  let {drug_category,page,pagesize} = req.query
  let startIndex = (page - 1) * 10
  let size = parseInt(pagesize)
  let sql = 'select * from drug where drug_category=? limit ?,?'
  pool.query(sql,[drug_category,startIndex,size],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 新增药品
router.post('/drug/add',(req,res) =>{
  let {drug_id,drug_name,durg_price,drug_category,drug_effect,drug_use,drug_bad,drug_term,drug_cover} = req.body
  let sql = 'insert into drug (drug_id,drug_name,durg_price,drug_category,drug_effect,drug_use,drug_bad,drug_term,drug_cover) values (?,?,?,?,?,?,?,?,?)'
  pool.query(sql,[drug_id,drug_name,durg_price,drug_category,drug_effect,drug_use,drug_bad,drug_term,drug_cover],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 修改药品
router.post('/drug/update',(req,res) =>{
  let {drug_name,durg_price,drug_category,drug_effect,drug_use,drug_bad,drug_term,drug_cover,drug_id} = req.body
  let sql = 'update drug set drug_name=?,durg_price=?,drug_category=?,drug_effect=?,drug_use=?,drug_bad=?,drug_term=?,drug_cover=? where drug_id=?'
  pool.query(sql,[drug_name,durg_price,drug_category,drug_effect,drug_use,drug_bad,drug_term,drug_cover,drug_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 删除药品
router.delete('/drug/delete',(req,res) =>{
  let {drug_id} = req.body
  let sql = 'delete from drug where drug_id=?'
  pool.query(sql,[drug_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

module.exports = router